<?php
/**
 * 装修公司
 */
namespace App\Http\ControllersAdmin;

use App\Service\NodesService;
use App\Validation\NodesValidation;
use App\Validation\NodesSearchValidation;
use App\Validation\NodesTreeValidation;
use App\Validation\DeleteValidation;

class NodesController extends BaseController
{
	/**
	 * [indexAction 列表]
	 * @return [type] [description]
	 */
	public function indexAction()
	{
		$this->getUser();
		$raw = $this->validation(new NodesSearchValidation, $this->request->getJsonRawBody());

		$data = NodesService::getList($raw);
		$count = NodesService::getList($raw, TRUE);

		$assign = [
			'total' => $count,
			'list'  => $data
		];
		return $this->echoJson($assign);
	}

	public function menuAction()
	{
        $users =$this->getUser();

        if($users->account!='admin'){
            $data = NodesService::getTreeNode(array('roleid'=>$users->role_id)) ;
        }else{
            $raw = $this->validation(new NodesTreeValidation, $this->request->getJsonRawBody());

            $data = NodesService::getTree($raw);
        }


		return $this->echoJson($data);
	}

	/**
	 * [createAction 创建]
	 * @return [type] [description]
	 */
	public function createAction()
	{
		$this->getUser();

		$raw = $this->validation(new NodesValidation, $this->request->getJsonRawBody());
		$data = NodesService::save($raw);
		return $this->echoJson('ok');
	}

	/**
	 * [detailAction 详情]
	 * @return [type] [description]
	 */
	public function detailAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = NodesService::getInfo($raw->id);
		return $this->echoJson($data);
	}

	/**
	 * [updateAction 更新]
	 * @return [type] [description]
	 */
	public function updateAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$raw = $this->validation(new NodesValidation, $this->request->getJsonRawBody());
		$data = NodesService::save($raw);
		return $this->echoJson('ok');
	}

	/**
	 * [upAction 上架]
	 * @return [type] [description]
	 */
	public function displayAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = NodesService::display($raw->id);
		return $this->echoJson('ok');
	}

	/**
	 * [deleteAction 删除]
	 * @return [type] [description]
	 */
	public function deleteAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = NodesService::forever($raw->id);
		return $this->echoJson('ok');
	}
}